//
//  StatusFrame.m
//  SinaWebo
//
//  Created by 亦心 on 15/3/15.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "StatusFrame.h"
#import "NSAttributedString+Extension.h"
#import "NSString+Extension.h"
#import "Status.h"
#import "User.h"
#import "EmotionTool.h"
#import "PhotosView.h"
#import "PageInfo.h"
#import "PicInfo.h"
#import "PicMetadata.h"

@implementation StatusFrame

-(void)setStatus:(Status *)status{
    _status=status;

    //cell的宽度
    CGFloat cellW=kScreenW-kCellMargin*2;
    //TopView
    CGFloat topViewX=0;
    CGFloat topViewY=0;
    CGFloat topViewW=cellW;
    CGFloat topViewH=0;
    
    //头像
    CGFloat iconViewX=StatusFrameMargin;
    CGFloat iconViewY=StatusHeaderTop;
    CGFloat iconViewWH=kPeopleWH;
    _iconViewF=CGRectMake(iconViewX, iconViewY, iconViewWH, iconViewWH);
    
    //认证图标
    CGFloat verifiedW = kVerified;
    CGFloat verifiedH = verifiedW;
    CGFloat verifiedX = CGRectGetMaxX(_iconViewF) - verifiedW;
    CGFloat verifiedY = CGRectGetMaxY(_iconViewF) - verifiedH;
    _verifiedF = CGRectMake(verifiedX, verifiedY, verifiedW, verifiedH);
    
     //昵称
    CGFloat nameLabelX=CGRectGetMaxX(_iconViewF)+StatusFrameMargin;
    CGFloat nameLabelY=iconViewY + StatusTimeTop;
    CGSize nameLabelSize=[status.user.screen_name sizeWithFont:StatusFrameNameLabelFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _nameLabelF=(CGRect){{nameLabelX,nameLabelY},nameLabelSize};
    
    //会员图标
    if (status.user.mbtype) {
        CGFloat vipViewX=CGRectGetMaxX(_nameLabelF)+StatusFrameMargin;
        CGFloat vipViewY=nameLabelY;
        CGFloat vipViewW=kVip;
        CGFloat vipViewH=nameLabelSize.height;
        _vipViewF=CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH);
    }
    
    //时间
    CGFloat timeLabelX= nameLabelX;
    CGFloat timeLabelY= CGRectGetMaxY(_nameLabelF) + StatusTimeTop;
    CGSize timeLabelSize=[status.created_at2 sizeWithFont:StatusFrameTimeLabelFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    _timeLabelF=(CGRect){{timeLabelX,timeLabelY},timeLabelSize};
    
    //来源
    CGFloat sourceLabelX=CGRectGetMaxX(_timeLabelF)+StatusFrameMargin;
    CGFloat sourceLabelY=timeLabelY;
    CGSize sourceLabelSize=[status.source sizeWithFont:StatusFrameTimeLabelFont];
    _sourceLabelF=(CGRect){{sourceLabelX,sourceLabelY},sourceLabelSize};
    
    //正文
    CGFloat contentLabelX=iconViewX;
    CGFloat contentLabelY=MAX(CGRectGetMaxY(_timeLabelF), CGRectGetMaxY(_iconViewF)) + StatusFrameMargin;
    CGFloat contentMaxW=topViewW-2*StatusFrameMargin;
   _textAttr = [[EmotionTool sharedEmotion] attributedString:status.text font:StatusFrameTextLabelFont status:status];
    CGFloat contentLabelH = [_textAttr attributedSize:CGSizeMake(contentMaxW, MAXFLOAT)].height;
    _contentLabelF= CGRectMake(contentLabelX, contentLabelY, contentMaxW, contentLabelH);
    
    //配图
    if (status.pic_ids.count) {
        CGSize photoViewSize=[PhotosView photosViewSizeWithDataArrray:status.pics];
        CGFloat photoViewX=contentLabelX;
        CGFloat photoViewY=CGRectGetMaxY(_contentLabelF)+StatusFrameMargin;
        _photoViewF=(CGRect){{photoViewX,photoViewY},photoViewSize};
        topViewH=CGRectGetMaxY(_photoViewF)+StatusFrameMargin;
    }else{
        //没有配图时微博的高度
        topViewH=CGRectGetMaxY(_contentLabelF)+StatusFrameMargin;
    }
    
    if (status.retweeted_status == nil && status.page_info && status.pic_ids.count == 0) {
        PageInfo *pageInfo = status.page_info;
        CGFloat pageInfoViewX = contentLabelX;
        CGFloat pageInfoViewY = CGRectGetMaxY(_contentLabelF)+StatusFrameMargin;
        CGFloat pageInforViewW = contentMaxW;
        CGFloat pageInforViewH;
        if (pageInfo.type == PageInfoTypeVideo) {
            PicMetadata *picMeta = pageInfo.pic_info.pic_small;
            CGFloat scal = picMeta.height / picMeta.width;
            if (scal > 0.8) {
                pageInforViewW = pageInforViewW * 0.7;
                pageInforViewH = pageInforViewW;
            }else{
                pageInforViewH = picMeta.height * pageInforViewW / picMeta.width;
            }
            _pageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, pageInforViewH);
        }else if (pageInfo.type == PageInfoTypeNews){
            PicMetadata *pic = [pageInfo.cards.firstObject pic_info].pic_big;
            if (pic == nil) {
                pageInfo.typeNewImgH = 200;
            }else{
                pageInfo.typeNewImgH = pic.height *pageInforViewW / pic.width;
            }
            NSString *pageTitle = pageInfo.page_title;
            CGFloat height = [pageTitle sizeWithFont:CardTitleLabelFont maxSize:CGSizeMake(pageInforViewW, MAXFLOAT)].height+20;
            _pageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, pageInfo.typeNewImgH+height);
        }else if (pageInfo.type == PageInfoTypeAtric){
            _pageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, 80);
        }else{
            _pageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, 50);
        }
        topViewH = CGRectGetMaxY(_pageInfoViewF) + StatusFrameMargin;
    }
    
    
    //被转发微博
    if (status.retweeted_status) {
        CGFloat retweetViewX=contentLabelX;
        CGFloat retweetViewY=CGRectGetMaxY(_contentLabelF)+StatusFrameMargin*0.5;
        CGFloat retweetViewW=kScreenW;
        CGFloat retweetViewH=0;
        
        //被转发微博的正文\内容
        CGFloat retweetContentLabelX=retweetViewX;
        CGFloat retweetContentLabelY=StatusFrameMargin;
        CGFloat retweetContentLabelMaxW=cellW - 2 * StatusFrameMargin;
        NSString *retText = [NSString stringWithFormat:@"@%@:%@",status.retweeted_status.user.screen_name, status.retweeted_status.text];
        _retweetedText = [[EmotionTool sharedEmotion] attributedString:retText font:StatusFrameRewTextLabelFont status:status];
        CGFloat retweetContentH = [_retweetedText attributedSize:CGSizeMake(retweetContentLabelMaxW, MAXFLOAT)].height;
        _retweetContentLabelF= CGRectMake(retweetContentLabelX,retweetContentLabelY, retweetContentLabelMaxW, retweetContentH);

        //被转发微博的配图
        if (status.retweeted_status.pic_ids.count) {
            CGFloat retweetPhotoViewX=retweetContentLabelX;
            CGFloat retweetPhotoViewY=CGRectGetMaxY(_retweetContentLabelF)+StatusFrameMargin;
            CGSize retweetPhotoView = [PhotosView photosViewSizeWithDataArrray:status.retweeted_status.pics];
            _retweetPhotoViewF=(CGRect){{retweetPhotoViewX,retweetPhotoViewY},retweetPhotoView};
            
            //有配图时被转发微博的高度
            retweetViewH=CGRectGetMaxY(_retweetPhotoViewF)+StatusFrameMargin;
        }else{
            retweetViewH=CGRectGetMaxY(_retweetContentLabelF)+StatusFrameMargin;
        }
        
        if (status.page_info && status.retweeted_status.pic_ids.count == 0) {
            PageInfo *pageInfo = status.page_info;
            CGFloat pageInfoViewX = retweetContentLabelX;
            CGFloat pageInfoViewY = CGRectGetMaxY(_retweetContentLabelF)+StatusFrameMargin;
            CGFloat pageInforViewW = retweetContentLabelMaxW;
            CGFloat pageInforViewH;
            if (pageInfo.type == PageInfoTypeVideo) {
                PicMetadata *picMeta = pageInfo.pic_info.pic_small;
                CGFloat scal = picMeta.height / picMeta.width;
                if (scal > 0.8) {
                    pageInforViewW = pageInforViewW * 0.7;
                    pageInforViewH = pageInforViewW;
                }else{
                    pageInforViewH = picMeta.height * pageInforViewW / picMeta.width;
                }
                _retpageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, pageInforViewH);
            } else if (pageInfo.type == PageInfoTypeNews){
                PicMetadata *pic = [pageInfo.cards.firstObject pic_info].pic_big;
                if (pic == nil) {
                    pageInfo.typeNewImgH = 200;
                }else{
                    pageInfo.typeNewImgH = pic.height *pageInforViewW / pic.width;
                }
                NSString *pageTitle = pageInfo.page_title;
                CGFloat height = [pageTitle sizeWithFont:CardTitleLabelFont maxSize:CGSizeMake(pageInforViewW, MAXFLOAT)].height+20;
                _retpageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, pageInfo.typeNewImgH+height);
            } else{
                _retpageInfoViewF = CGRectMake(pageInfoViewX, pageInfoViewY, pageInforViewW, 50);
            }
            
            retweetViewH = CGRectGetMaxY(_retpageInfoViewF) + StatusFrameMargin;
        }
        
        _retweetViewF=CGRectMake(0, retweetViewY, retweetViewW, retweetViewH);
        //有转发微博时微博的高度
        topViewH=CGRectGetMaxY(_retweetViewF);
    }
    
    _topViewF=CGRectMake(topViewX, topViewY, topViewW, topViewH);
    
    //工具条
    CGFloat statusToolbarX=topViewX;
    CGFloat statusToolbarY=CGRectGetMaxY(_topViewF);
    CGFloat statusToolbarW=topViewW;
    CGFloat statusToolbarH=36;
    _statusToolbarF=CGRectMake(statusToolbarX, statusToolbarY, statusToolbarW, statusToolbarH);
    
    //cell的高度
    _cellHeight=CGRectGetMaxY(_statusToolbarF)+10;
}


@end
